package com.ilxqx.framework.system.converter.bind;

import com.ilxqx.framework.system.enumeration.EntityEnum;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;

/**
 * EntityEnum枚举转换工厂
 * @author venus
 */
@Slf4j
public class EntityEnumConverterFactory implements ConverterFactory<String, EntityEnum> {

	/**
	 * Get the converter to convert from S to target type T, where T is also an instance of R.
	 *
	 * @param targetType the target type to convert to
	 * @return a converter from S to T
	 */
	@NotNull
	@Override
	public <T extends EntityEnum> Converter<String, T> getConverter(@NotNull Class<T> targetType) {
		return new EntityEnumConverter<T>(targetType);
	}

	private static class EntityEnumConverter<T extends EntityEnum> implements Converter<String, T> {

		private Class<T> cls;

		public EntityEnumConverter(Class<T> cls) {
			this.cls = cls;
		}
		/**
		 * Convert the source object of type {@code S} to target type {@code T}.
		 *
		 * @param source the source object to convert, which must be an instance of {@code S} (never {@code null})
		 * @return the converted object, which must be an instance of {@code T} (potentially {@code null})
		 * @throws IllegalArgumentException if the source cannot be converted to the desired target type
		 */
		@Override
		public T convert(@NotNull String source) {
			return EntityEnum.getEnum(this.cls, source);
		}

	}

}
